import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { VersioningType } from '@nestjs/common';
import * as session from 'express-session'
import * as cors from 'cors' //解除跨域第三方中间件
import { Request, Response, NextFunction } from 'express'
import { NestExpressApplication } from '@nestjs/platform-express'
import { join } from 'path';
import { CommonResponse } from './common/response'
import { HttpFilter } from './common/filter'
import { ValidationPipe } from '@nestjs/common'
// import { RoleGuard } from './guard/role/role.guard';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { WsAdapter } from '@nestjs/platform-ws';

const whiteList = ['/list'];

function MiddleWAareAll(req:Request,res:Response,next:NextFunction){
    // console.log(req.originalUrl);
    // if(whiteList.includes(req.originalUrl)){
    //   next();
    // }else{
    //   res.send({message:"关进小黑屋！"});
    // }
}

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule,{cors:true});

  // websocket
  app.useWebSocketAdapter(new WsAdapter(app));

  const options = new DocumentBuilder().addBearerAuth().setTitle('小满接口文档').setDescription('很小的飞机').setVersion('1').build();
  const document = SwaggerModule.createDocument(app,options)
  SwaggerModule.setup('/api-docs',app,document)

  app.use(cors()); //baidu控制台:fetch('http://localhost:3000/user/123').then(res=>res.json()).then(res=>{console.log(res);})
  app.useStaticAssets(join(__dirname,'images'),{
    prefix:"/songtao.xiang" //静态资源自定义虚拟目录
  });//配置静态资源访问目录
  app.enableVersioning({ //开启api版本控制
    type:VersioningType.URI
  });
  app.use(session({secret:"xiaoman",rolling:true,name:"xiaoman.sid",cookie:{maxAge:9999999999}}));
  // app.use(MiddleWAareAll); //请求路由过滤器
  app.useGlobalFilters(new HttpFilter()); //全局异常拦截器
  app.useGlobalInterceptors(new CommonResponse()); //全局响应拦截器
  app.useGlobalPipes(new ValidationPipe()); // 全局自带验证DTO管道
  // app.useGlobalGuards(new RoleGuard());//全局路由守卫

  await app.listen(3100);

}
bootstrap();

/**
 * 3.Code码规范
    200 OK
    304 Not Modified 协商缓存了
    400 Bad Request 参数错误
    401 Unauthorized token错误
    403 Forbidden referer origin 验证失败
    404 Not Found 接口不存在
    500 lnternal Server Error服务端错误
    502 Bad Gateway 上游接口有问题或者服务器问题
 */